package com.yourname.www.frame.dialog;

import com.yourname.www.dao.JdbcHelper;
import com.yourname.www.entity.*;
import com.yourname.www.frame.LoginFrame;
import com.yourname.www.frame.RegisterFrame;
import com.yourname.www.listen.LoginButtonActionListener;
import com.yourname.www.util.Tools;
import com.yourname.www.util.WindowUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

/**
 * 添加学生对话框
 */
public class AddStudentDialog extends JDialog {
    private JDialog jf;
    //性别下拉选项
    private JComboBox jComboBoxSex;
    //年级
    private JComboBox jComboBoxGrade;
    //院系
    private JComboBox jComboBoxDepartment;
    //专业
    private JComboBox jComboBoxMajor;
    //班级
    private JComboBox jComboBoxClasses;

    //设置两个输入框
    private JTextField usernameText;        //用户名输入框
    //设置两个标签
    private JLabel usernameLabel;	//用户名标签。
    private JLabel sexLabel;	//性别标签。
    private JLabel gradeLabel;	//年级标签。
    private JLabel departmentLabel;	//院系标签。
    private JLabel majorLabel;	//专业标签。
    private JLabel classLabel;	//班级标签。
    //设置两个按钮
    private JButton confirmButton;	//返回登录按钮。
    private JButton cancelButton;	//注册按钮。

    private final String DEFAULT_OPTION = "--请选择--";

    private int width = 85;
    private int top = 60;
    private int height = 40;

    private  Container c;
    private JdbcHelper jdbcHelper = new JdbcHelper();
    public AddStudentDialog(JFrame jFrame){
        super(jFrame,true);
        setBasicInfo();
        this.setLayout(null);
        c = this.getContentPane();
        usernameLabel =new JLabel("姓名");
        usernameLabel.setBounds(width,top,80,30);
        c.add(usernameLabel);
        usernameText = new JTextField();
        usernameText.setBounds(width+50,top,80,30);
        c.add(usernameText);

        sexLabel =new JLabel("性别");
        sexLabel.setBounds(width,top+50,80,30);
        c.add(sexLabel);
        jComboBoxSex = new JComboBox(new String[]{DEFAULT_OPTION,"男","女"});
        jComboBoxSex.setBounds(width+50,top+50,80,30);
        c.add(jComboBoxSex);

        gradeLabel =new JLabel("年级");
        gradeLabel.setBounds(width,top+100,80,30);
        c.add(gradeLabel);
        jComboBoxGrade = new JComboBox(Tools.CreateGrade());
        jComboBoxGrade.setBounds(width+50,top+100,80,30);
        c.add(jComboBoxGrade);


        departmentLabel =new JLabel("院系");
        departmentLabel.setBounds(width,top+150,80,30);
        c.add(departmentLabel);
        //SQL:先读取所有院系 【实体集合、Map】
        jComboBoxDepartment = new JComboBox();
        List<Department> allDepartment = jdbcHelper.getAllDepartment();
        //第一个选项
        jComboBoxDepartment.addItem(DEFAULT_OPTION);
        //对列表遍历
        allDepartment.forEach((s)->{
            //添加下拉选项
            jComboBoxDepartment.addItem(s.getDepartment_Name());
        });

        jComboBoxDepartment.setBounds(width+50,top+150,160,30);
        c.add(jComboBoxDepartment);
        //监听下拉选项的事件


        majorLabel =new JLabel("专业");
        majorLabel.setBounds(width,top+200,80,30);
        c.add(majorLabel);

        jComboBoxMajor = new JComboBox();
        jComboBoxMajor.setBounds(width+50,top+200,160,30);
        c.add(jComboBoxMajor);
        //当用户选择了什么院系之后，才能确定要显示什么专业
        //  SQL:根据院系名称读取对应的专业【实体集合、Map】
        //当院系变化时，修改【专业、班级】可选项
        jComboBoxDepartment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("院系选择："+jComboBoxDepartment.getSelectedItem().toString());
                //以防用户的选项是'--请选择----'
                //定义专业下拉的选项
                //先清空原有的专业
                jComboBoxMajor.removeAllItems();
                jComboBoxMajor.addItem(DEFAULT_OPTION);
                jComboBoxClasses.removeAllItems();
                jComboBoxClasses.addItem(DEFAULT_OPTION);
                //.....
                List<Major> byDepartmentName = jdbcHelper.getMajorByDepartmentName(jComboBoxDepartment.getSelectedItem().toString());
                if(byDepartmentName !=null && byDepartmentName.size()>0){
                    byDepartmentName.forEach((s)->{
                        //添加下拉选项
                        jComboBoxMajor.addItem(s.getMajor_Name());
                    });
                }



            }
        });



        classLabel =new JLabel("班级");
        classLabel.setBounds(width,top+250,80,30);
        c.add(classLabel);
        jComboBoxClasses = new JComboBox();
        jComboBoxClasses.setBounds(width+50,top+250,160,30);
        c.add(jComboBoxClasses);
        //SQL 根据年级和专业ID读取班级【实体集合、Map】
        //当年专业变化时，动态修改可选项
        jComboBoxMajor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("专业选择："+jComboBoxMajor.getSelectedItem().toString());
                //以防用户的选项是'--请选择----'
                //定义专业下拉的选项
                //先清空原有的专业
                jComboBoxClasses.removeAllItems();
                jComboBoxClasses.addItem(DEFAULT_OPTION);
                //.....
                try {
                    Integer.valueOf(jComboBoxGrade.getSelectedItem().toString());
                    if(jComboBoxMajor.getSelectedItem() == null) throw new NumberFormatException();
                }catch (NumberFormatException exception){

                    return;
                }
                List<Classe> byDepartmentName = jdbcHelper.getClasseByGradeAndMajorID(Integer.valueOf(jComboBoxGrade.getSelectedItem().toString()),jComboBoxMajor.getSelectedItem().toString());
                if(byDepartmentName!=null && byDepartmentName.size() >0){
                    byDepartmentName.forEach((s)->{
                        //添加下拉选项
                        jComboBoxClasses.addItem(s.getClasse());
                    });
                }


            }
        });
        //当年级变化时，动态修改可选项
        jComboBoxGrade.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("专业选择："+jComboBoxMajor.getSelectedItem().toString());
                //以防用户的选项是'--请选择----'
                //定义专业下拉的选项
                //先清空原有的专业
                jComboBoxClasses.removeAllItems();
                jComboBoxClasses.addItem(DEFAULT_OPTION);
                //.....
                try {
                    // 避免出现异常
                    Integer.valueOf(jComboBoxGrade.getSelectedItem().toString());
                    if(jComboBoxMajor.getSelectedItem() == null) throw new NumberFormatException();
                }catch (NumberFormatException exception){

                    return;
                }
                List<Classe> byDepartmentName = jdbcHelper.getClasseByGradeAndMajorID(Integer.valueOf(jComboBoxGrade.getSelectedItem().toString()),jComboBoxMajor.getSelectedItem().toString());
                //保证有数据才执行
                if(byDepartmentName!=null && byDepartmentName.size() >0){
                    byDepartmentName.forEach((s)->{
                        //添加下拉选项
                        jComboBoxClasses.addItem(s.getClasse());
                    });
                }


            }
        });
        //确定 取消
        //监听鼠标事件
        confirmButton = new JButton("确定");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("提交");
                //获取用户名和密码
                String username = usernameText.getText().trim();
                if("".equals(username)){
                    //弹出提示消息
                    JOptionPane.showMessageDialog(jf, "用户名不能为空","系统提醒",JOptionPane.WARNING_MESSAGE);
                    return ;
                }

                try{
                    String sex = jComboBoxSex.getSelectedItem().toString();
                    String grade = jComboBoxGrade.getSelectedItem().toString();
                    String department = jComboBoxDepartment.getSelectedItem().toString();
                    String major = jComboBoxMajor.getSelectedItem().toString();
                    String classes = jComboBoxClasses.getSelectedItem().toString();
                    //有任何一个选项是请选择，抛出异常
                    if(DEFAULT_OPTION.equals(sex) || DEFAULT_OPTION.equals(grade)||DEFAULT_OPTION.equals(department)||DEFAULT_OPTION.equals(major)||DEFAULT_OPTION.equals(classes)){
                            throw new Exception();
                    }

                    //连接数据库 检查用户名是否存在 检查密码是否正确

                    Student user = new Student();
                    user.setStudent_Name(username);
                    user.setStudent_Sex(sex);
                    user.setClasse(Integer.valueOf(classes));//班级
                    user.setDepartment_Name(department);//院系
                    user.setMajor_Name(major);//专业名称
                    user.setGrade(Integer.valueOf(grade));

                    if(jdbcHelper.insertStudent(user)){
                        int optionType = JOptionPane.showConfirmDialog(jf, "添加成功,是否继续添加", "系统提醒", JOptionPane.INFORMATION_MESSAGE);
                        System.out.println(optionType);
                        if(optionType == JOptionPane.YES_OPTION){
                            usernameText.setText("");
                        }else{
                            jf.dispose();
                        }

                    }else{
                        JOptionPane.showMessageDialog(jf, "注册失败，请联系991850594@qq.com","系统提醒",JOptionPane.WARNING_MESSAGE);
                    }

                }catch (Exception exception){
                    exception.printStackTrace();
                    JOptionPane.showMessageDialog(jf, "请完善输入信息","系统提醒",JOptionPane.WARNING_MESSAGE);
                    return ;

                }

            }
        });
        confirmButton.setBounds(100,300+top,80,30);
        c.add(confirmButton);
        cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //关闭当前窗口
                jf.dispose();
                //打开新的窗口
            }
        });
        cancelButton.setBounds(250,300+top,80,30);
        c.add(cancelButton);
        this.setVisible(true);

    }
    //设置基本信息
    private void setBasicInfo() {
        this.jf = this;
        this.setTitle("添加学生");
        //设置Logo
        try{
            Image image = ImageIO.read(this.getClass().getResource("/logo.png"));
            this.setIconImage(image);
        }catch (IOException e) {
            e.printStackTrace();
        }
        //设置大小
        this.setSize(400,550);
        //设置是否可以调节大小
        this.setResizable(false);
        //设置关闭行为【其实默认就是，可以不设置，了解方法即可】
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置居中
        WindowUtil.setFrameCenter(this);
    }

}
